/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.service.impl;

import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.ShopContextHolder;
import com.medusa.gruul.integral.api.entity.IntegralRule;
import com.medusa.gruul.integral.api.entity.IntegralShop;
import com.medusa.gruul.integral.api.model.DiscountInfoDto;
import com.medusa.gruul.integral.api.model.ShopFunctionInfoVo;
import com.medusa.gruul.integral.mapper.IntegralRuleMapper;
import com.medusa.gruul.integral.mapper.IntegralShopMapper;
import com.medusa.gruul.integral.service.IIntegralShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.integral.web.enums.IntegralRuleGainTypeEnum;
import com.medusa.gruul.integral.web.model.dto.manager.IntegralShopDto;
import com.medusa.gruul.integral.web.model.vo.IntegralShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 积分商城 服务实现类
 * </p>
 *
 * @author xiaoq
 * @since 2019-11-19
 */
@Service
public class IntegralShopServiceImpl extends ServiceImpl<IntegralShopMapper, IntegralShop>
		implements IIntegralShopService {
	@Autowired
	private IntegralShopMapper integralShopMapper;

	@Autowired
	private IntegralRuleMapper integralRuleMapper;


	/**
	 * 修改积分商城开启关闭状态
	 * @param shopId 商铺id (城市合伙人)
	 * @param status 状态值
	 */
	@Override
	public void updateIntegralShopStatus(String shopId, Boolean status) {
		Integer number = integralShopMapper.updateShopStatus(shopId, status);
		if (number != 1) {
			throw new ServiceException("数据错误", SystemCode.FAILURE.getCode());
		}
	}


	/**
	 * 获取积分商城信息
	 * @param shopId 商铺id (城市合伙人)
	 * @return
	 */
	@Override
	public IntegralShopVo getIntegralShopInfo(String shopId) {
		IntegralShopVo shopInfo = integralShopMapper.getShopInfo(shopId);
		return shopInfo;
	}

	/**
	 *
	 * @param integralShopDto
	 */
	@Override
	public Boolean addShopInfo(IntegralShopDto integralShopDto) {
		IntegralShop integralShop = integralShopDto.coverIntegralShop();
		String shopId = ShopContextHolder.getShopId();
		//默认关闭积分商城
		integralShop.setType(false);
		Long aLong = integralShopMapper.selectByShopId(shopId);
		if (aLong != null) {
			throw new ServiceException("数据错误", SystemCode.FAILURE.getCode());
		}
		Integer insert = integralShopMapper.insert(integralShop);
		if (insert == 0) {
			throw new ServiceException("默认积分商城信息插入失败", SystemCode.DATA_ADD_FAILED.getCode());
		}
		return true;
	}

	/**
	 * 商家积分抵扣规则
	 * @param shopId
	 * @return
	 */
	@Override
	public DiscountInfoDto getDiscountInfo(String shopId) {
		return integralShopMapper.getDiscountInfo(shopId);
	}


	/**
	 * 获取所有商城信息
	 *
	 * @return 查询所有积分商城
	 */
	@Override
	public List<IntegralShop> getAllShopInfo() {
		return integralShopMapper.selectAllShopInfo();
	}

	@Override
	public boolean saveIntegralShopDefault(IntegralShop integralShop) {
		int i = integralShopMapper.insertShopDefault(integralShop);
		if (i < 1) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}


	/**
	 * 获取商城功能信息  商城信息 步数信息 签到信息
	 *
	 * @param shopId 商铺id
	 * @return
	 */
	@Override
	public ShopFunctionInfoVo getShopFunctionInfo(String shopId) {
		//商城信息
		IntegralShopVo shopInfo = integralShopMapper.getShopInfo(shopId);
		if (null == shopInfo) {
			throw new ServiceException("商城信息不存在", SystemCode.DATA_NOT_EXIST.getCode());
		}
		IntegralRule singInRule = integralRuleMapper
				.getRuleStatus(shopInfo.getId(), IntegralRuleGainTypeEnum.SING_IN.getStatus());
		if (null == singInRule) {
			throw new ServiceException("积分商城规则信息不存", SystemCode.DATA_NOT_EXIST.getCode());
		}
		IntegralRule stepNumberRule = integralRuleMapper
				.getRuleStatus(shopInfo.getId(), IntegralRuleGainTypeEnum.STEP_NUMBER.getStatus());
		if (null == stepNumberRule) {
			throw new ServiceException("积分商城规则信息不存", SystemCode.DATA_NOT_EXIST.getCode());
		}
		//封装
		ShopFunctionInfoVo vo = new ShopFunctionInfoVo();
		vo.setShop(shopInfo.getType());
		vo.setSingIn(singInRule.getOpen());
		vo.setStepNumber(stepNumberRule.getOpen());
		return vo;
	}
}
